package net.lab1024.sa.admin.module.app.api.device;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.app.api.device.domain.DeviceAddForm;
import net.lab1024.sa.admin.module.device.control.DeviceExecuteCommandService;
import net.lab1024.sa.admin.module.device.terminal.DeviceTerminalService;
import net.lab1024.sa.admin.module.device.terminal.domain.DeviceTerminalEntity;
import net.lab1024.sa.admin.module.system.config.SystemConfigService;
import net.lab1024.sa.admin.module.upstream.box.cases.UpstreamServiceUseCase;
import net.lab1024.sa.base.common.constant.SystemKeyConst;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * app 设备业务接口
 */
@Slf4j
@Service
public class AppDeviceService {

    private final DeviceTerminalService deviceTerminalService;

    private final UpstreamServiceUseCase upstreamServiceUseCase;

    private final SystemConfigService systemConfigService;

    private final DeviceExecuteCommandService deviceExecuteCommandService;

    public AppDeviceService(DeviceTerminalService deviceTerminalService,
                            UpstreamServiceUseCase upstreamServiceUseCase,
                            SystemConfigService systemConfigService,
                            DeviceExecuteCommandService deviceExecuteCommandService) {
        this.deviceTerminalService = deviceTerminalService;
        this.upstreamServiceUseCase = upstreamServiceUseCase;
        this.systemConfigService = systemConfigService;
        this.deviceExecuteCommandService = deviceExecuteCommandService;
    }

    /**
     * 添加设备
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(Long userId, DeviceAddForm form) {
        if (userId == null) {
            return ResponseDTO.userErrorParam("获取用户ID异常");
        }
        String serialno = form.getSerialno();
        DeviceTerminalEntity deviceTerminalEntity = deviceTerminalService.getBySerialno(serialno);
        if (deviceTerminalEntity == null) {
            return ResponseDTO.userErrorParam("设备未上报");
        }
        if (deviceTerminalEntity.getUserId() != null) {
            return ResponseDTO.userErrorParam("设备已被绑定");
        }
        Boolean linkFlag = deviceTerminalEntity.getLinkFlag();
        if (!linkFlag) {
            return ResponseDTO.userErrorParam("设备未联网");
        }
        // 远程连接检查
        Boolean remoteLink = deviceTerminalService.checkRemoteLink(serialno);
        if (!remoteLink) {
            return ResponseDTO.userErrorParam("设备连接丢失，请联网后等待5分钟");
        }
        // 标记用户
        deviceTerminalService.setUserIdBySerialno(userId, serialno);
        // 得到默认部署的上游
        String defaultUpstream = systemConfigService.getValueByKey(SystemKeyConst.DefaultConfig.UPSTREAM);
        if (defaultUpstream != null) {
            Integer upstream = Integer.valueOf(defaultUpstream);
            deviceExecuteCommandService.deploy(serialno, upstream, deviceTerminalEntity.getOsType());
        }
        return ResponseDTO.ok();
    }
}
